package com.uwo.commons.configuration.aop.validate.validate;

import com.uwo.commons.configuration.aop.validate.ErrorCode;
import com.uwo.commons.configuration.aop.validate.ValidateField;
import com.uwo.commons.configuration.aop.validate.ValidateProperties;
import com.uwo.commons.configuration.aop.validate.annotation.Sensitive;
import com.uwo.commons.kits.SensitiveKits;
import com.uwo.commons.configuration.aop.validate.excepteion.ApiValidateExceptrion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 敏感字段校验
 * @author hao.yan
 */
@Component(value = "Validate_Sensitive")
public class SensitiveValidate extends BaseValidate{

    @Autowired
    private ValidateProperties validateProperties;

    @Override
    public void validate(Annotation annotation, ValidateField validateField) throws ApiValidateExceptrion {
        super.validate(annotation, validateField);
        if (validateField.getValue() == null)
            return;
        // 判断是否存在敏感字符
        if (SensitiveKits.validateSensitiveWords(String.valueOf(validateField.getValue()))) {
            Sensitive sensitive = (Sensitive)annotation;
            // 属性配置
            String message = value(validateProperties.getMessages(), sensitive.message(),
                    ErrorCode.SENSITIVE_WORD_ERROR.getMessage(validateField.getName()));
            exception(sensitive.code(), message, ErrorCode.SENSITIVE_WORD_ERROR);
        }
    }

}
